package lukeer.fencing.lukeer.fencing.old;

import lukeer.fencing.Board;
import lukeer.fencing.BoardMove;
import lukeer.fencing.Piece;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import static java.lang.Thread.sleep;

/**
 * Created with IntelliJ IDEA.
 * User: Luke Richardson, Wafa M'Barek
 * Date: 30/09/12
 * Time: 15:17
 */
public class UserGUI implements Runnable {
    private static final int ROW = 6;
    private static final int COLUMNS = 6;
    private static final Color[] COLOR = {null, Color.BLACK, Color.WHITE};
    private static final String[] PLAYER = {null, "BLACK", "WHITE"};
    private Board b;
    private GameBoard boardDisplay;
    private JTextField statusField = new JTextField();

    private Piece.Type player;

    private Board.Direction direction;
    private int col;
    private int row;
    private int fCol;
    private int pCol;
    private int fRow;
    private int pRow;
    private boolean hasFence;

    public boolean isReady() {
        return isReady;
    }

    public void registerMove() {
        isReady = true;
    }

    private boolean isReady;

    private JButton upButton = new JButton("Up");
    private JButton downButton = new JButton("Down");
    private JButton leftButton = new JButton("Left");
    private JButton rightButton = new JButton("Right");
    private JButton pieceButton = new JButton("Place Piece");
    private JButton noFenceButton = new JButton("No Fence");
    private JButton finishButton = new JButton("Finished Move");

    public UserGUI(Board b, Piece.Type player) {
        this.player = player;
        JFrame window = new JFrame("Fencing game");
        boardDisplay = new GameBoard();
        window.setLayout(new BorderLayout());
        JPanel buttons1 = new JPanel(new GridLayout());
        JPanel buttons2 = new JPanel(new GridLayout());
        JPanel bottomBar = new JPanel(new GridLayout(2,1));
        buttons2.add(leftButton);
        buttons2.add(upButton);
        buttons2.add(downButton);
        buttons2.add(rightButton);
        buttons2.add(noFenceButton);
        buttons1.add(pieceButton);
        buttons1.add(finishButton);
        window.add(buttons1, BorderLayout.NORTH);
        window.add(boardDisplay, BorderLayout.CENTER);
        bottomBar.add(buttons2);
        bottomBar.add(statusField);
        window.add(bottomBar, BorderLayout.SOUTH);
        window.pack();
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setLocationRelativeTo(null);
        window.setResizable(false);
        window.setVisible(true);
        this.b=b;
        upButton.addActionListener(new DirectionButtonAction());
        downButton.addActionListener(new DirectionButtonAction());
        leftButton.addActionListener(new DirectionButtonAction());
        rightButton.addActionListener(new DirectionButtonAction());
        noFenceButton.addActionListener(new DirectionButtonAction());
        pieceButton.addActionListener(new PlacePieceButtonAction());
        finishButton.addActionListener(new FinishMoveButtonAction());
    }

    public void updateStatus() {
        statusField.setText("(" + col + ", " + row + ")" + " Piece: " + "(" + pCol + "," + pRow + ")" + " Fence: " + "(" + fCol + "," + fRow + ")" +
                 direction);
    }

    public BoardMove getLastmove() {
        return lastmove;
    }

    private BoardMove lastmove;

    @Override
    public void run() {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    class GameBoard extends JComponent implements MouseListener {
        private static final int CELL_SIZE = 50;
        private static final int WIDTH = COLUMNS * CELL_SIZE;
        private static final int HEIGHT = ROW * CELL_SIZE;

        public GameBoard() {
            this.setPreferredSize(new Dimension(WIDTH, HEIGHT));
            this.addMouseListener(this);
        }

        @Override
        public void paintComponent(Graphics g) {
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setColor(Color.LIGHT_GRAY);
            g2.fillRect(0,0,WIDTH,HEIGHT);
            g2.setColor(Color.BLACK);
            for(int r=1; r<ROW; r++) {
                g2.drawLine(0, r*CELL_SIZE, WIDTH, r*CELL_SIZE);
            }
            for(int c=1; c< COLUMNS; c++) {
                g2.drawLine(c*CELL_SIZE, 0, c*CELL_SIZE, HEIGHT);
            }
            for(int r = 0; r < ROW; r++) {
                for(int c = 0; c < COLUMNS; c++) {
                    int x = c * CELL_SIZE;
                    int y = r * CELL_SIZE;
                    Piece.Type piece = b.getPiece(x/CELL_SIZE,y/CELL_SIZE).getType();
                    if(piece!= Piece.Type.EMPTY) {
                        g2.setColor(piece== Piece.Type.BLACK?Color.BLACK:Color.WHITE);
                        g2.fillOval(x+2, y+2, CELL_SIZE-4, CELL_SIZE-4);
                    }
                }
            }
        }

        public void mousePressed(MouseEvent e) {
            col = e.getX()/CELL_SIZE;
            row = e.getY()/CELL_SIZE;
            updateStatus();
            this.repaint();
        }

        public void mouseClicked(MouseEvent e) {}
        public void mouseReleased(MouseEvent e) {}
        public void mouseEntered(MouseEvent e) {}
        public void mouseExited(MouseEvent e) {}


    }

    private class DirectionButtonAction implements ActionListener {
        @Override public void actionPerformed(ActionEvent e) {
            if(e.getActionCommand() == "Up") direction = Board.Direction.UP;
            if(e.getActionCommand() == "Down") direction = Board.Direction.DOWN;
            if(e.getActionCommand() == "Left") direction = Board.Direction.LEFT;
            if(e.getActionCommand() == "Right") direction = Board.Direction.RIGHT;
            if(e.getActionCommand() == "No Fence") direction = null;
            fCol = col;
            fRow = row;
            updateStatus();
        }
    }

    private class PlacePieceButtonAction implements ActionListener {
        @Override public void actionPerformed(ActionEvent e) {
            pCol = col;
            pRow = row;
            updateStatus();
        }
    }

    private class FinishMoveButtonAction implements ActionListener {
        @Override public void actionPerformed(ActionEvent e) {
            System.out.println(col + " " + row);
            if(direction != null) lastmove = new BoardMove(col, row, player,fCol, fRow, direction);
                else {
                    lastmove = new BoardMove(col, row, player);
                }
            isReady = true;
            }
        }
    }
//        }


